package no.netlight.gotogate;

import java.net.URL;
import java.util.ArrayList;

import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;

public class SpreadsheetHandler {
	public SpreadsheetHandler() {

	}

	public ArrayList<String[]> fetchInfo(String url) {
		String contents;
		ArrayList<String[]> data = new ArrayList<String[]>();
		String[] strFromSpreadsheet;
		ArrayList<String> strs;
		String str;
		try {
			DocsService service = new DocsService("Result");
			URL spreadsheetFeedUrl = new URL(url);
			SpreadsheetFeed feed = service.getFeed(spreadsheetFeedUrl,
					SpreadsheetFeed.class);

			if(feed == null){
				return null;
			}
			
			for (SpreadsheetEntry entry : feed.getEntries()) {
				contents = entry.getPlainTextContent();
				strFromSpreadsheet = contents.split(",");
				strs = new ArrayList<String>();
				for (int i = 0; i < strFromSpreadsheet.length; i++) {
					str = strFromSpreadsheet[i].substring(strFromSpreadsheet[i].indexOf(":") + 1).trim();
					if(i>2 && str.length() < 4){
						str = "0"+str;
					}
					strs.add(str);
				}
				data.add((String[]) strs.toArray(new String[strs.size()]));
			}
		} catch (Exception e) {
			System.err.println("Exception: " + e.getMessage());
		}finally{

		}
		return data;
	}
}
